% Data for US-ROW case
% clear all
% close all
% setup_workspace
[num,txt,raw] = xlsread('USA_ROW.xlsx');

% data are from 1974 to 2019Q4 but missing data for hours for ROW from
% 2016Q4
% with Interest Rate, data need to start fromm 1979Q2 (Ts=22)

% order of variables

% 1 Year
% 2 Quarter
% 3 YQ
% 4 gr RGDP US
% 5 gr PGDP US
% 6 gr RCon US
% 7 gr RInv US
% 8 gr Price of imports US
% 9 gr Price of Exports US
% 10 gr Hours US
% 11 gr TFP (annual) US 
% 12 NX/Y US
% 13 REER US
% 14 gr RGDP ROW
% 15 gr PGDP ROW
% 16 gr Rcon ROW
% 17 gr RInv ROW
% 18 gr Price of imports ROW
% 19 gr Price of exports ROW
% 20 gr Hours ROW
% 21 US Interest Rate 
% 22 ROW Interest Rate (no data using all 13 countries available)
% 23 ROW Interest Rate starting from 1974 (adjusted weights for missing)
% 24 US 5 year yield
% 25 US 10 yeard yield
% 26 ROW 5 year yield
% 27 ROW 10 year yield
% 28 US spread
% 29 ROW spread
% 30 US real exports
% 31 US real imports
% 32 gr of TFP ROW
% 33 gr of TFP USA we constructed
% 34 gr of TFP ROW unadjusted 
% 35 gr of TFP USA unadjusted 
% 36 Global factor from Helene Ray Restud paper
% 37 Commodity price weigh by export shares of commodity
% 38 4q ahead annual growth rate of output 
% 39 4q ahead annual growth rate of consumption
% 40 constructed real exchange rate (average of quarter (from IMF NOT CORRECT))
% 41 End of period short term rate US (3 month yield)
% 42 End of period short term rate ROW
% 43 Inflation rate CPI US
% 44 Inflation rate CPI ROW
% 45 End of period consructed real exchange rate (from IMF NOT CORRECT)
% 46 End of period constructed RER using BIS (CORRECT)
% 47 Average daily constructed RER using BIS (CORRECT)
% 48 haver daily constructed rer
% 49 new EOP constructed RER using BIS (multiplication) <- we use
% 50 new average constructed rer using BIS (multiplication)
% 51 haver rer
% 52 realized vix
% 53 ted spread using intrate1 (short term fed funds
% 54 ted spread using end of period yield and average libor 
% 55 ted spread using end of period yield and end of period libor
% 56 price index for energy
% 57 price index for non energy 
% 58 price index for agriculture
% 59 price index for precious metals
% 60 N export/ gdp
% 61 N imports/ gdp
% 62 Nominal exchange rate (End of period constructed NER - counterpart to 49)
% 63 real stock returns of the US
% 64 real trade balance
% 65 real trade balance/GDP
% 66 Terms of trade

% [num1,txt1,raw1]=xlsread('spread.xlsx',2); % spread data
% 
% num1=num1(1:end-2,:); % 1974Q1 to 2019Q4 
% 

Ts=1; %1974Q1 
% Ts=22; % 1979Q2
Te=172; % 2016Q4

% [num3,txt3,raw3]=xlsread('data_CCI.xlsx'); % consumer confidence data 

Ts1=64; %1989Q4 to do with Forecast
Ts_f=1; %1989Q4
Te_f=109; % 2016Q4

num=num(Ts:Te,:);
% num1=num1(Ts:Te,:);
% num3=num3(Ts:Te,:); 

% accumulate to log level for US and ROW
usdata_gr=num(:,[4 6 7 8 9 10 43]);
rowdata_gr=num(:,[14 16 17 18 19 20 44]);

usdata=cumsum(usdata_gr);
rowdata=cumsum(rowdata_gr);

% accumulate to log level for US real exports and real imports
usdata_gr1=num(:,[30 31]);
usdata1=cumsum(usdata_gr1);
logtfp_row=cumsum(num(:,32));
num(1,33)=0; % missing first TFP constructed for the US
logtfp_usa=cumsum(num(:,33)); % our constructed TFP
logtfpua_usa=cumsum(num(:,35));
logtfpua_row=cumsum(num(:,34));


% tfp data
a=num(:,11)/400;
logtfp=cumsum(a);

% labor productivity
lp_us=usdata(:,1)-usdata(:,6);
lp_row=rowdata(:,1)-rowdata(:,6);

% Interest Rate
intratediff=(num(:,21)-num(:,23))/100/4; % i-i_row
intratediff1=(num(:,21)-num(:,23))/100/4; % i-i_row from 1974
intratediff5=(num(:,24)-num(:,26))/100/4; % i-i_row 5 year
intratediff10=(num(:,25)-num(:,27))/100/4; % i-i_row 10 year
intratediffnew=(num(:,41)-num(:,42))/100/4; % i-i_row EOP

% dRER
dRER=log(num(:,49))-lagmatrix(log(num(:,49)),1);
dRER(1)=NaN;
% 
% % spread
% % spread=num1(:,3)/100/4 ; % us spread
% spread=(num(:,28)-num(:,29))/100/4 ; % us - row spread
% GF=num(:,36);
% X=log(num(:,37));

data1=[usdata(:,1)-rowdata(:,1) ... % y-y_row
    usdata(:,2)-rowdata(:,2) ... % c-c_row
    usdata(:,6)-rowdata(:,6) ... % h-h_row
    num(:,43) - num(:,44) ... % pi-pi_row
    usdata(:,3)-rowdata(:,3) ... % inv - inv_row
    num(:,12) ... % nxy
    log(num(:,49))... % REER
    intratediffnew ...  % i-i_row
    ]; 

db.data1=data1';
db.data_name={'Output','Consumption','Hours Worked',...
                          'Inflation','Investment','NXY','REER','Interest Rate'};

% data1_tfp=[data1 ...
%         logtfp-logtfp_row ... % fernald tfp - row tfp    
%         ];
%  db.data1_tfp=data1_tfp';
%  db.data_tfp_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','REER','Interest Rate','TFP'};
% 
%  
% dataREER=[usdata(:,1)-rowdata(:,1) ... % y-y_row
%     usdata(:,2)-rowdata(:,2) ... % c-c_row
%     usdata(:,6)-rowdata(:,6) ... % h-h_row
%     num(:,43) - num(:,44) ... % pi-pi_row
%     usdata(:,3)-rowdata(:,3) ... % inv - inv_row
%     num(:,12) ... % nxy
%     log(num(:,13))... % REER
%     intratediffnew];  % i-i_row
% 
% db.dataREER=dataREER';
% 
% dataNER=[usdata(:,1)-rowdata(:,1) ... % y-y_row
%     usdata(:,2)-rowdata(:,2) ... % c-c_row
%     usdata(:,6)-rowdata(:,6) ... % h-h_row
%     num(:,43) - num(:,44) ... % pi-pi_row
%     usdata(:,3)-rowdata(:,3) ... % inv - inv_row
%     num(:,12) ... % nxy
%     log(num(:,62))... % NER
%     intratediffnew ...  % i-i_row
%     ]; 
% 
% db.dataNER=dataNER';
% db.dataNER_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','NER','Interest Rate'};
% 
% data_dRER=[usdata(:,1)-rowdata(:,1) ... % y-y_row
%     usdata(:,2)-rowdata(:,2) ... % c-c_row
%     usdata(:,6)-rowdata(:,6) ... % h-h_row
%     num(:,43) - num(:,44) ... % pi-pi_row
%     usdata(:,3)-rowdata(:,3) ... % inv - inv_row
%     num(:,12) ... % nxy
%     dRER ... % REER
%     intratediffnew];  % i-i_row
% 
% db.data_dRER=cutnan2(data_dRER)';
% db.data_dRER_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','dRER','Interest Rate'};
% 
%  data_rtby=[data1 ...
%         num(:,65)]; 
% db.data_rtby=cutnan2(data_rtby)'; 
% db.data_rtby_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','REER','Interest Rate','Real TBY'};
%  data_tot=[data1 ...
%         num(:,66)]; 
% db.data_tot=cutnan2(data_tot)'; 
% db.data_tot_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','REER','Interest Rate','TOT'};
% 
% 
% % wedge
%                       
% data_f=[data1 ...
%         num(:,38)/400]; 
% db.data_f=cutnan2(data_f)'; 
% db.data_f_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','REER','Interest Rate','Forecast'};
% data_consconf=[data1 ...
%                 log(num3(:,8))];
% db.data_consconf=cutnan2(data_consconf)'; 
% db.data_consconf_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','REER','Interest Rate','Consumer Confidence'};
% 
% data_spread=[data1 ...
%                 spread ]; % spread
% db.data_spread_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','REER','Interest Rate','Spread'};
% data_spread=cutnan2(data_spread);
% db.data_spread=data_spread'; % transpose
% 
% data_GF=[data1 ...
%             GF];
% data_GF=cutnan2(data_GF);
% db.data_GF=data_GF';
% db.data_GF_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','REER','Interest Rate','Global Factor'};
% 
% data_commodity=[data1 ...
%             X];
% data_commodity=cutnan2(data_commodity);
% db.data_commodity=data_commodity';
% db.data_commodity_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','REER','Interest Rate','Commodity Price'};
% 
% data_vix=[data1 ...
%             num(:,52)];
%         data_vix=cutnan2(data_vix);
%         db.data_vix=data_vix';
%         db.data_vix_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','REER','Interest Rate','VIX'};
% data_stock=[data1 ...
%             num(:,63)];
%         data_stock=cutnan2(data_stock);
%         db.data_stock=data_stock';
%         db.data_stock_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','REER','Interest Rate','Stock returns'};
% 
% data_ted=[data1 ...
%             num(:,55)];
%         data_ted=cutnan2(data_ted);
%         db.data_ted=data_ted';
%         db.data_ted_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','REER','Interest Rate','TED spread'};
% 
% % with P/P*
% data_p=[usdata(:,1)-rowdata(:,1) ... % y-y_row
%     usdata(:,2)-rowdata(:,2) ... % c-c_row
%     usdata(:,6)-rowdata(:,6) ... % h-h_row
%     usdata(:,7)-rowdata(:,7) ... % log(P)-log(P*)
%     usdata(:,3)-rowdata(:,3) ... % inv - inv_row
%     num(:,12) ... % nxy
%     log(num(:,49))... % REER
%     intratediffnew];  % i-i_row
% db.data_p=data_p';
% db.data_p_name={'Output','Consumption','Hours Worked',...
%                           'CPI','Investment','NXY','REER','Interest Rate'};
% 
% % with exports and imports
% data_expimp=[usdata(:,1)-rowdata(:,1) ... % y-y_row
%     usdata(:,2)-rowdata(:,2) ... % c-c_row
%     usdata(:,6)-rowdata(:,6) ... % h-h_row
%     num(:,43) - num(:,44) ... % pi-pi_row
%     usdata(:,3)-rowdata(:,3) ... % inv - inv_row
%     log(num(:,49))... % REER
%     intratediffnew ...  % i-i_row
%     usdata1(:,1) ... % real exports
%     usdata1(:,2)];  % real imports
% db.data_expimp_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','REER','Interest Rate', 'Exports','Imports'};
% db.data_expimp=data_expimp'; % transpose
%     
% data_yield5=[data1 ...   
%     intratediff5];  % i-i_row  5 year
% data_yield5=cutnan2(data_yield5);
% db.data_yield5=data_yield5'; % transpose
% db.data_yield5_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','REER','interest rate','5 year Yield'};
% 
% data_yield10=[data1 ...
%     intratediff10];  % i-i_row  10 year
% data_yield10=cutnan2(data_yield10);
% db.data_yield10=data_yield10'; % transpose
% db.data_yield10_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','REER','interest rate','10 year yield'};
% 
% % individual country data
% data_absolute=[usdata(:,1) ... % y
%     usdata(:,2) ... % c
%     usdata(:,6) ... % h
%     num(:,43) ... % pi
%     num(:,12) ... % nxy
%     log(num(:,49))... % REER
%     num(:,41)/400 ...  % i
%     logtfp ... US TFP
%     rowdata(:,1) ... % y_row
%     rowdata(:,2) ... % c_row
%     rowdata(:,6) ... % h_row
%     num(:,44) ... % pi_row
%     num(:,42)/400 ... % i_row
%     logtfp_usa ... % constructed US
%     logtfp_row ... % row
%     usdata(:,3) ... % i
%     rowdata(:,3) ... % investment
%     (num(:,28))/400 ... % us spread
%     (num(:,29))/100/4 ...  %  row spread
%     num(:,52) ... % vix
%     num(:,60) ... % export/gdp
%     num(:,61) ... % import/gdp
%     ];
% db.data_absolute_name={'US Output','US Consumption','US Hours Worked',...
%                           'US Inflation','US NXY','US REER','US interest rate', 'US TFP', ...
%                           'ROW Output','ROW Consumption','ROW Hours', ...
%                           'ROW Inflation','ROW Interest rate', 'Our US TFP','Our ROW TFP','US Inv','ROW Inv',...
%                           'US spread','ROW spread','VIX','Exports/GDP','Imports/GDP'};
% 
% db.data_absolute=data_absolute'; % transpose
% 
% data_pcom=[data1 ...
%         log(num(:,56)) ...
%         log(num(:,57)) ...
%         log(num(:,58)) ...
%         log(num(:,59))];
%     
%         
%  db.data_pcom=data_pcom';
%  db.data_pcom_name={'Output','Consumption','Hours Worked',...
%                           'Inflation','Investment','NXY','REER','Interest Rate','energy price','non energy price', ...
%                           'agriculture price','metal price'};
% 
% %% subperiod
% Tbreak=133;
% Tbreak_GF=109;
% 
% db.data1_sub1=db.data1(:,Tbreak:end);
% db.data_GF_sub1=db.data_GF(:,Tbreak_GF:end);
% % db.data_sub1=db.data(:,Tbreak:end);
% 
% db.data1_sub2=db.data1(:,1:Tbreak-1);
% db.data1_GF_sub2=db.data_GF(:,1:Tbreak_GF-1);
% % db.data_sub2=db.data(:,1:Tbreak-1);
% 
% %% plot
% close all
% 
% figure()
% for i=1:size(data1,2)
% subplot(3,4,i)
% plot(db.data1(i,:))
% title(db.data_name{i})
% end
% 
% 



